import javax.xml.soap.Node;

public class Binarytree {
    public static void main(String[] args) {
    Hero root=new Hero(1,"宋江");
    Hero node2=new Hero(2,"卢俊义");
    Hero node3=new Hero(3,"林冲");
    Hero node4=new Hero(4,"李逵");
    root.setLeft(node2);
    root.setRight(node3);
    node3.setRight(node4);
    Tree tree = new Tree();
    tree.SetTree(root);
    tree.Prefixsort();
        System.out.println();
    tree.Infixsort();
        System.out.println();
    tree.Suffixsort();
        System.out.println();
    tree.proorderSearch(4);

        System.out.println();
    tree.delHero(1);
    tree.Suffixsort();
    }
}
class Tree{
    private Hero root;

    public void SetTree(Hero root) {
        this.root = root;
    }
    public void Prefixsort(){
        if (this.root!=null){
            this.root.Prefixsort();
        }else {
            System.out.println("失败");
        }
    }
    public void Infixsort(){
        if (this.root!=null){
            this.root.Infixsort();
        }else {
            System.out.println("失败");
        }
    }
    public void Suffixsort(){
        if (this.root!=null){
            this.root.Suffixsort();
        }else {
            System.out.println("失败");
        }
    }


    public void proorderSearch(int no){
        if (this.root!=null){
            System.out.println(this.root.PreorderSearch(no));
        }else {
            System.out.println("失败");
        }
    }
    public void delHero(int no){
        if (this.root.getNo()==no){
            this.root=null;
        }else {
            this.root.delHero(no);
        }

    }

}
class Hero{
    private int no;
    private String name;
    private Hero left;
    private Hero right;

    public Hero(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public Hero getLeft() {
        return left;
    }

    public void setLeft(Hero left) {
        this.left = left;
    }

    public Hero getRight() {
        return right;
    }

    public void setRight(Hero right) {
        this.right = right;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void Prefixsort(){
        System.out.println(this);
        if (this.left!=null){
            this.left.Prefixsort();
        }
        if (this.right!=null){
            this.right.Prefixsort();
        }
    }

    @Override
    public String toString() {
        return "Hero{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void Infixsort(){
        if (this.left!=null){
            this.left.Infixsort();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.Infixsort();
        }
    }
    public void Suffixsort(){
        if (this.left!=null){
            this.left.Suffixsort();
        }
        if (this.right!=null){
            this.right.Suffixsort();
        }
        System.out.println(this);
    }
    public Hero PreorderSearch(int no){
        Hero hero=null;
        if (this.no==no){
            return this;
        }
        if (this.left!=null){
            hero=this.left.PreorderSearch(no);
        }
        if (hero!=null){
            return hero;
        }
        if (this.right!=null){
            hero=this.right.PreorderSearch(no);
        }
        if (hero!=null){
            return hero;
        }
        return hero;
    }
    public void delHero(int no){
        if (this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delHero(no);
        }
        if (this.right!=null){
            this.right.delHero(no);
        }
    }
}
